package file;

import java.io.*;

/**
 * ObjectInoutStream类
 *    ObjectInputStream 对以前使用 ObjectOutputStream 写入的基本数据和对象进行反序列化。
 */
public class Student implements Serializable{//实现序列接口Serializable
    private String name ;
    private Integer age;
    private  String addr;

    public Student() {
    }

    public Student(String name, Integer age, String addr) {
        this.name = name;
        this.age = age;
        this.addr = addr;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAddr() {
        return addr;
    }

    public void setAddr(String addr) {
        this.addr = addr;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", addr='" + addr + '\'' +
                '}';
    }
}
class Demo4{
    public static void main(String[] args){
        File file =new File("e:\\xx.txt");

        Student s1=new Student("孙悟空",23,"昆明五华区");
        Student s2=new Student("猪八戒",23,"昆明五华区");
        Student s3=new Student("小白龙",23,"昆明五华区");
        Student s4=new Student("沙和尚",23,"昆明五华区");
        try {
            ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(s1);
            oos.writeObject(s2);
            oos.writeObject(s3);
            oos.writeObject(s4);
            oos.close();
            ObjectInputStream ois=new ObjectInputStream(new FileInputStream(file));
            while (true) {
                try {
                    Student s=(Student) ois.readObject();
                    System.out.println(s);
                } catch (EOFException e) {
                    break;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }
}
